package com.gxgeek.javabasic.config;

import com.gxgeek.javabasic.activemq.springjms.customer.ConsumerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;

/**
 * Created by Administrator on 2017/7/13.
 */
@Configuration
public class CommonBean {

    @Autowired
    private ConsumerService consumerService;
    private static final String ActiveMQ_URL = "tcp://127.0.0.1:61616";
    private static final String ActiveMQ_USER = "admin";
    private static final String ActiveMQ_PASSWORD = "admin";
    @Bean
    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(ActiveMQ_URL);
        connectionFactory.setUserName(ActiveMQ_USER);
        connectionFactory.setPassword(ActiveMQ_PASSWORD);
        return connectionFactory;
    }

    /**
     * 目的地队列
     * @return
     */
    @Bean("queueDestination")
    public Destination queueDestination(){
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("spring-queue");
        return activeMQQueue;
    }

    /**
     * 配置消息容器
     * @return
     */
    @Bean("jmsContainer")
    public DefaultMessageListenerContainer listenerContainer(){
        DefaultMessageListenerContainer messageConsumer = new DefaultMessageListenerContainer();
        messageConsumer.setConnectionFactory(connectionFactory());
        messageConsumer.setDestination(topicDestination());
        messageConsumer.setMessageListener(consumerService);
        return messageConsumer;
    }




    @Bean
    public JmsTemplate jmsQueueTemplate(){
        return new JmsTemplate(connectionFactory());
    }






    //主题模式

    /**
     *主题目的地址
     * @return
     */
    @Bean("topicDestination")
    public Destination topicDestination(){
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("spring-topic");
        return activeMQTopic;
    }










}
